//
//  UserCareerController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/30.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserCareerController.h"
#import "UserCareerExpModel.h"
#import "UserCareerAwardCell.h"
#import "UserCareerExpCell.h"


#define ksectionHeadHeight kAutoHei(30)
#define ksectionFootHeight kAutoHei(400)

@interface UserCareerController ()

/** 生涯列表数据 */
@property (nonatomic, strong) NSMutableArray *careerlists;

@end

@implementation UserCareerController{
    BOOL _isChinese;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isChinese = [NSBundle currentLanguageIsChinese];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"UserCareerAwardcell" bundle:nil] forCellReuseIdentifier:@"UserCareerAwardCell"];
    
    [self setTableviewStyle];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self loadData];
}

- (void)loadData {
    
    [self.careerlists removeAllObjects];
    self.careerlists = nil;
    __weak typeof(self) weakSelf = self;
    NSMutableArray *arrM = [NSMutableArray array];
    
    [UserCareerExpModel requestCarceerExpListDatacompletion:^(NSArray *exps) {
         __block UserExpSameYearModel *temp = nil;
    
        [exps enumerateObjectsUsingBlock:^(UserCareerExpModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj.Year isEqualToString:temp.year]) {// 同一年的
                [temp.careerArr addObject:obj];
            }else {
                temp = [[UserExpSameYearModel alloc] init];
                temp.year = obj.Year;
                
                if (temp.careerArr.count > 0) {
                    [temp.careerArr removeAllObjects];
                    temp.careerArr = nil;
                }
                [temp.careerArr addObject:obj];
                [arrM addObject:temp];
            }
            
        }];
        
        NSArray *sortExps = [arrM.copy sortedArrayUsingComparator:^NSComparisonResult(UserExpSameYearModel *_Nonnull obj1,UserExpSameYearModel * _Nonnull obj2) {
            
            return [obj1.year compare:obj2.year];
        }];
       
        [weakSelf.careerlists addObjectsFromArray:sortExps];
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView reloadData];
    }];
}


#pragma mark 设置tableView的样式
- (void)setTableviewStyle {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    // 设置背景色
    self.tableView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    // 去掉分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 300;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }else {
       return self.careerlists.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserCareerAwardCell" forIndexPath:indexPath];
        UserCareerAwardCell *awardCell = (UserCareerAwardCell *)cell;
        awardCell.targetVc = self;
        awardCell.userModel = self.userModel;
    }else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserCareerExpCell" forIndexPath:indexPath];
        UserCareerExpCell *expCell = (UserCareerExpCell *)cell;
        expCell.expModel = self.careerlists[indexPath.row];
        
        if (indexPath.row == 0) {
            [expCell showTopLineview:NO];
        }else {
            [expCell showTopLineview:YES];
        }
        
        if (indexPath.row == self.careerlists.count - 1) {
            expCell.isLastLine = YES;
        }else {
            expCell.isLastLine = NO;
        }
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 150;
    }else {
        UserExpSameYearModel *expModel = self.careerlists[indexPath.row];
        if (indexPath.row == self.careerlists.count - 1) {
            return expModel.rowHeight;
        }else {
            return expModel.rowHeight - 32;
        }

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return ksectionHeadHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1 && self.careerlists.count <= 0) {
        return ksectionFootHeight;
    }else {
        return 0.01;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, ksectionHeadHeight)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kYFScreenWidth - 20, headView.bounds.size.height)];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor colorWithHex:0x151515 alpha:0.5];
    [headView addSubview:label];
    headView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    if (section == 0) {
        label.text = NSLocalizedString(@"Medal", @"勋章");
    }else if (section == 1){
        label.text =NSLocalizedString(@"Experience", @"经历");
    }
    return headView;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 1 && self.careerlists.count <= 0) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, ksectionFootHeight)];
        
        UIImage *image = [UIImage imageNamed:@"wushuju-0"];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kYFScreenWidth - image.size.width) * 0.5, 20, image.size.width, image.size.height)];
        imageView.image = image;
        [footerView addSubview:imageView];
        footerView.backgroundColor = [UIColor whiteColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame), kYFScreenWidth, 30)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = NSLocalizedString(@"You haven't added experience yet", @"您还没有添加网球履历哦");
        label.textColor = [UIColor colorWithHex:0x787878];
        label.font = [UIFont systemFontOfSize:15];
        
        [footerView addSubview:label];
        
        // 创建分割线
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, 0.5)];
        topView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.15];
        
        [footerView addSubview:topView];
        
        return footerView;
    }else {
        return [[UIView alloc] init];
    }
}

#pragma mark - getter & setter 
- (NSMutableArray *)careerlists {
    if (_careerlists == nil) {
        _careerlists = [NSMutableArray array];
    }
    return _careerlists;
}

@end
